package com.blb.jpa_demo.dao;

import com.blb.jpa_demo.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * JPA操作接口
 */
@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Long> {

    /**
     * 查询年龄区间的员工
     * @param min
     * @param age
     * @return
     */
    List<Employee> findByAgeBetween(Long min,Long age);

    /**
     * 姓名模糊查询
     * @param name
     * @return
     */
    List<Employee> findByNameContains(String name);

    /**
     * 名字包含某个字的年龄最大的前三人
     */
    List<Employee> findTop3ByNameContainsOrderByAgeDesc(String name);

    /**
     * 原生SQL的配置
     * @param employee
     */
    @Transactional
    @Modifying
    @Query(value = "update tb_employee set name=:#{#e.name},age=:#{#e.age},address=:#{#e.address},gender=:#{#e.gender},dept_id=:#{#e.deptId} where id =:#{#e.id}",
    nativeQuery = true)
    void update(@Param("e") Employee employee);
}
